<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :maskClosable="false"
    :style="modalStyle"
    @cancel="handleCancel"
    cancelText="关闭">
    <template slot="footer">
      <a-button key="back" @click="handleCancel">取消</a-button>
    </template>
    <a-form :form="form">
      <template>
        <a-card :bordered="false">
          即将导出{{excelCount}}条数据。如需导出明细数据（可能耗时较长），请勾选下方复选框。<br/>
          <a-checkbox :checked="checked" @change="handleChange">需要导出明细数据</a-checkbox>
          <div style="padding-top: 20px; text-align: center">
            <a-button type="primary" @click="handleExportXlsEx(excelType)">导出数据</a-button>
          </div>
        </a-card>
      </template>
      <template>
        <a-row>
          <a-col>
            <a-form-item>
              <a-input v-decorator="['id']" hidden/>
            </a-form-item>
          </a-col>
        </a-row>
      </template>
    </a-form>
  </a-modal>
</template>

<script>
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { getMpListShort } from "@/utils/util"
  import { getAction } from '@/api/manage'
  import Vue from 'vue'

  export default {
    name: 'BillExcel',
    mixins:[JeecgListMixin],
    data () {
      return {
        title: "单据导出",
        width: '500px',
        visible: false,
        model: {},
        form: this.$form.createForm(this),
        disableMixinCreated: true,
        modalStyle: '',
        excelCount: 0,
        excelType: '',
        checked: false,
        queryParam: {},
        url: {
          exportXlsUrl: "/api/plugin/stock-check/stockCheck/exportStockCheckExcel",
          getBillInfo: "/api/plugin/stock-check/stockCheck/getBillInfo",
        }
      }
    },
    methods: {
      show(search) {
        this.visible = true
        this.modalStyle = 'top:20%; height: 400px;'
        if(search) {
          this.queryParam = JSON.parse(search)
        }
        //扩展属性
        this.queryParam.mpList = getMpListShort(Vue.ls.get('materialPropertyList'))
        this.queryParam.hasDetail = '0'
        //获取单据的条数
        getAction(this.url.getBillInfo, this.queryParam).then((res) => {
          if (res.code === 200) {
            this.excelCount = res.data.billCount
            this.excelType = res.data.billType
          } else if (res.code === 505) {
            this.$message.warning(res.data)
          }
        })
      },
      //切换勾选
      handleChange(e) {
        this.checked = e.target.checked
        this.queryParam.mpList = getMpListShort(Vue.ls.get('materialPropertyList'))
        if(this.checked) {
          this.queryParam.hasDetail = '1'
        } else {
          this.queryParam.hasDetail = '0'
        }
      },
      handleExportXlsEx(fileName){
        if(this.excelCount <= 1000) {
          this.handleExportXls(fileName)
        } else {
          this.$message.warning('抱歉，单次最多只能导出1000条，建议筛选单据日期！')
        }
      },
      handleCancel() {
        this.close()
      },
      close() {
        this.$emit('close');
        this.visible = false;
        this.modalStyle = ''
      }
    }
  }
</script>